#sk-kPce0mzlnwFUX5LP9KI80VXpdhPMR0gkkzXS0TY08iIZogkQ
from flask import Blueprint, request, jsonify
from openai import OpenAI

client = OpenAI(
    api_key="sk-kPce0mzlnwFUX5LP9KI80VXpdhPMR0gkkzXS0TY08iIZogkQ",
    base_url="https://api.moonshot.cn/v1",
)

history = [
    {"role": "system",
     "content": "你是 Kimi，由 Moonshot AI 提供的人工智能助手，你更擅长中文和英文的对话。你会为用户提供安全，有帮助，准确的回答。同时，你会拒绝一切涉及恐怖主义，种族歧视，黄色暴力等问题的回答。Moonshot AI 为专有名词，不可翻译成其他语言。"}
]


def chat(query, history):
    history.append({
        "role": "user",
        "content": query
    })
    completion = client.chat.completions.create(
        model="moonshot-v1-8k",
        messages=history,
        temperature=0.3,
    )
    result = completion.choices[0].message.content
    history.append({
        "role": "assistant",
        "content": result
    })
    return result


ai_bp = Blueprint('ai_bp', __name__)


@ai_bp.route('/ai', methods=['POST'])
def ai():

    data = request.get_json()
    query = data.get('query', [])
    historys = data.get('historys', [])

    if not query:
        return jsonify({"error": "No queries provided"}), 400
    
    for one in historys:
        history.append({
            "role": "assistant",
            "content": one
        })

    response = chat(query, history)

    # 返回最后一次的结果
    return jsonify({"response": response}), 200
